﻿using MSSClient.Utils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using TestTreeView.Model;

namespace MSSClient
{
    [Serializable]
    /// <summary>
    /// Interaction logic for ResultDlg.xaml
    /// </summary>
    public partial class ImageEdit : Window
    {
        public delegate void MergeImage(String imageUrl, String Url1, String Url2);
        public MergeImage mergeImage;

        private String imageUrl1;
        private String imageUrl2;
        private String changeImageUrl = "";
        private int mode = 2;//1-上下，2-左右
        private int changeMode = 0;

        public ImageEdit(String url1, String url2)
        {
            imageUrl1 = url1;
            imageUrl2 = url2;
            changeImageUrl = CommonUtil.MergeImage(0, 0, 1, imageUrl1, imageUrl2);
            changeMode = 1;
            InitializeComponent();
            loadImage(changeImageUrl);
            
        }

        private void loadImage(String path)
        {
                imagePreview.Source = CommonUtil.Bitmap2BitmapImageFromImagrPath(path);
                GC.Collect(); //强制回收资源  
                GC.WaitForPendingFinalizers();
        }

        private void exchange_Button_Click(object sender, RoutedEventArgs e)
        {
            if (mode.Equals(1) && changeMode.Equals(0))
            {                
                changeImageUrl = CommonUtil.MergeImage(1, 1, 0, imageUrl1, imageUrl2);
                changeMode = 1;
            }
            else if(mode.Equals(1) && changeMode.Equals(1))
            {
                changeImageUrl = CommonUtil.MergeImage(1, 1, 0, imageUrl2, imageUrl1);
                changeMode = 0;
            }

            if (mode.Equals(2) && changeMode.Equals(0))
            {               
                changeImageUrl = CommonUtil.MergeImage(1, 0, 1, imageUrl1, imageUrl2);
                changeMode = 1;
            }
            else if (mode.Equals(2) && changeMode.Equals(1))
            {
                changeImageUrl = CommonUtil.MergeImage(1, 0, 1, imageUrl2, imageUrl1);
                changeMode = 0;
            }
            
            loadImage(changeImageUrl);
        }

        private void upDwon_Button_Click(object sender, RoutedEventArgs e)
        {
            mode = 1;
            if (changeMode == 0)
            {
                changeImageUrl = CommonUtil.MergeImage(0, 1, 0, imageUrl1, imageUrl2);
                changeMode = 1;
            }
            else
            {
                changeImageUrl = CommonUtil.MergeImage(0, 1, 0, imageUrl2, imageUrl1);
                changeMode = 0;
            }
            loadImage(changeImageUrl);
        }

        private void leftRight_Button_Click(object sender, RoutedEventArgs e)
        {
            mode = 2;
            if (changeMode == 0)
            {
                changeImageUrl = CommonUtil.MergeImage(0, 0, 1, imageUrl1, imageUrl2);
                changeMode = 1;
            }
            else
            {
                changeImageUrl = CommonUtil.MergeImage(0, 0, 1, imageUrl2, imageUrl1);
                changeMode = 0;
            }
            loadImage(changeImageUrl);
        }

        private void queding_Button_Click(object sender, RoutedEventArgs e)
        {
            mergeImage(changeImageUrl, imageUrl1, imageUrl2);
            Close();
        }

        private void close_Button_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
    }
}
